$(function () {
    var isExchange =true;
    $("body").on("click","#exchange",function(){
      var that =  $(this);


        var flag =false;
        //检查是否选择了地址
        $.ajax({
            url: baseloaction + "/mobile/uc/userInfo/getListAddressAjax",
            type: "post",
            dataType: "json",
            success: function (result) {
                if(result!=null&&result!=undefined&&result.length>0){
                    flag =true;
                }
                exchangeShop(that,flag);
            },
            error: function (error) {
                Zepto.toast("系统繁忙,请稍后重试");
            }
        });
    });


    function  exchangeShop(that,flag){
   if(that.data("type")=="ENTITY"){
         if(flag){
    //如果没有选前往页面进行选择  ""
            Zepto.confirm("当前商品为实体商品，是否使用当前默认地址邮寄？",function () {
                exchangeIntegralShop(that)
            },function () {
                sessionStorage.goBacUrl=window.location.href;
                window.location.href="/mobile/uc/userInfo/updateUserAddress";
            });
            Zepto('.modal-in').find('.modal-button').text('否').css('color', '#bbb');
            Zepto('.modal-in').find('.modal-button-bold').text('是').attr('style', '');
        }else{
            Zepto.confirm("当前商品为实体商品，需要设置收获地址，是否去设置 ？",function () {
                sessionStorage.goBacUrl=window.location.href;
                window.location.href="/mobile/uc/userInfo/updateUserAddress";
            },function () {

            });
            Zepto('.modal-in').find('.modal-button').text('否').css('color', '#bbb');
            Zepto('.modal-in').find('.modal-button-bold').text('是').attr('style', '');
        }


        }else{
       exchangeIntegralShop(that)
   }
    }


    function exchangeIntegralShop(that){

        that.text("兑换中");
        that.removeClass("cInfo-price-bx");
        if(!true){
            Zepto.toast( "频繁操作");
            return;
        }
        isExchange =false;
        var id = that.data("id");
        $.ajax({
            url: baseloaction + "/mobile/uc/integral/exchangeIntegralShop",
            data: {"id": id},
            type: "post",
            dataType: "json",
            success: function (result) {
                setTimeout(function(){
                    Zepto.toast(result.message);
                         window.location.href =        baseloaction+ "/mobile/uc/integral/integralGiftIndex?type=2"
                },400)

            },
            error: function (error) {
                Zepto.toast("系统繁忙,请稍后重试");
            }
        })
    }

});